$(function(){
	page.bind();
	queryApprovalProcessSet();
	page.removeBtnBind();
})
var approver_id_arr = [];
var approver_id_arr_old = [];
var table;
var page = {
	bind:function(){
		$("#updateModal .left-box").on("click","#searchBtn",function(){
			var searchText = $("#searchText").val();
			ajaxUtil.ajaxRequest("basicData/getUserSimpleInfo",{'searchText':searchText}).then(function(data){
				$(".left-box").html("");
				$("#updateModal .left-box").html("<input id='searchText' type='text' placeholder='部门/车间/姓名/工号' style='width:140px;'/><span id='searchBtn' class='btn'>搜索</span>");
				var html="";
				for(var i=0;i<data.length;i++){
					html+="<li class='item ui-sortable-handle' value='"+data[i].userId+"' >"+data[i].userName+"</li>";
				}
				$(".left-box").append(html);
				page.leftAndRightBind();
				for(var i in approver_id_arr_old){
					$(".left-box").find('[value="'+approver_id_arr_old[i]+'"]').remove();
				}
				$("#searchText").val(searchText);
			});
		});
		//点击删除按钮
		$('#queryTable tbody').on("click",'.delete',function(){
			var typeStr = $(this).parents().children('td').eq(0).text();
			var type = "";
			switch(typeStr){
			case '请假审批':type=1;break;
			case '补卡审批':type=2;break;
			case '加班审批':type=3;break;
			case '报销审批':type=4;break;
			case '行政处罚审批':type=5;break;
			case '行政奖励审批':type=6;break;
			}
			if(confirm("确定删除该审批流程设置?")){
				ajaxUtil.ajaxRequest("dingTalk/deleteApprovalProcessSet",{'type':type}).then(function(data){
					table.ajax.reload( null, false);
					layer.msg(data,{icon:1});
				});
			}
		});
		//模态框隐藏
		$("#updateModal").on("hidden.bs.modal",function(){
			$(".left-box").append($(".right-box").find('.item').removeClass('selected-item'));
		});
		//点击新增按钮 加载所有用户
		$("#add").bind("click",function(){
			ajaxUtil.ajaxRequest("user/queryAllUser",{}).then(function(data){
				var html="";
				for(var i=0;i<data.length;i++){
					if(i%7==0){
						html+="<tr><td><input type='checkbox' name='addUserId' value='"+data[i].id+"' />"+data[i].userName+"</td>";
					}else{
						html+="<td><input type='checkbox' name='addUserId' value='"+data[i].id+"' />"+data[i].userName+"</td>";
					}
				}
				$("#all-user").html(html);
			});
			$("#addModal").modal("show");
		});
		//点击修改按钮 打开修改模态框
		$('#queryTable tbody').on("click",'.update',function(){
			var type = $(this).parents().children('td').eq(0).text();
			switch(type){
			case '请假审批':type=1;break;
			case '补卡审批':type=2;break;
			case '加班审批':type=3;break;
			case '报销审批':type=4;break;
			case '行政处罚审批':type=5;break;
			case '行政奖励审批':type=6;break;
			}
			var approverIds = $(this).val();
			approver_id_arr = approverIds.split(",");
			approver_id_arr_old = approverIds.split(",");
			ajaxUtil.ajaxRequest("basicData/getUserSimpleInfo",{}).then(function(data){
				$(".left-box").html("");
				$("#updateModal .left-box").html("<input id='searchText' type='text' placeholder='部门/车间/姓名/工号' style='width:140px;'/><span id='searchBtn' class='btn'>搜索</span>");
				var html="";
				for(var i=0;i<data.length;i++){
					html+="<li class='item ui-sortable-handle' value='"+data[i].userId+"' >"+data[i].userName+"</li>";
				}
				$(".left-box").append(html);
				page.leftAndRightBind();
				for(var i in approver_id_arr_old){
					$(".right-box").append($(".left-box").find('[value="'+approver_id_arr_old[i]+'"]'));
				}
				$("#updateApprovalBtn").val(type);
				$("#updateModal").modal("show");
			});
			
		});
		//点击添加模态框确定按钮 添加审批流程
		$("#addApprovalBtn").bind("click",function(){
			var type=$("#typeSelect option:selected").val();
			var idArray=new Array();
			//获取选中行
			$("[name='addUserId']:checked").each(function(){  
				idArray.push($(this).val());
			}); 
			if(type==""){
				layAlert("请选择审批类型");
				return false;
			}
			if(idArray.length<=0){
				layAlert("请勾选要审核的人员");
				return false;
			}
			ajaxUtil.ajaxRequest("approval/addApprovalProcess",{"type":type,"idArray":JSON.stringify(idArray)}).then(function(data){
				layer.msg(data,{icon:1});
				table.ajax.reload( null, false);
				$("#addModal").modal("hide");
			});
		});
		//点击修改模态框的确定按钮 修改可审批人
		$("#updateApprovalBtn").bind("click",function(){
			var type = $(this).val();
			//比对approver_id_arr，数组中没有的则用于添加，数组中有但是selected中没有的则删除
			var addArr = [];//用于添加
			var subArr = [];//用于删除
			var itemArr =[];
			$(".right-box li.item").each(function(){
				var val = $(this).attr("value");
				if($.inArray(val,approver_id_arr)==-1){
					addArr.push(val);
				}
				itemArr.push(val);
			});	
			if(itemArr.length<=0){
				layAlert("请选择可审批人");
				return false;
			}
			for(var x in approver_id_arr){
				if($.inArray(approver_id_arr[x],itemArr)==-1){
					subArr.push(approver_id_arr[x]);
				}
			}
			ajaxUtil.ajaxRequest("dingTalk/addAndDeleteApprovalProcess"
					,{"type":type,"addArray":JSON.stringify(addArr)
						,'subArray':JSON.stringify(subArr)})
							.then(function(data){
				table.ajax.reload( null, false);
				$("#updateModal").modal("hide");
				layer.msg(data,{icon:1});
			});
		});
		
	},
	leftAndRightBind:function(){
		$(".item").unbind("click");
		$(".left-box .item").unbind("dblclick");
		$(".right-box .item").unbind("dblclick");
		//单击
		$(".item").bind("click",function(){
			$(this).toggleClass("selected-item");
		});
		//左边双击右移
		$(".left-box .item").bind("dblclick",function(){
			$(".right-box").append($(this).removeClass('selected-item'));
			getSelected();
			page.leftAndRightBind();
		});
        //右边双击左移
		$(".right-box .item").bind("dblclick",function(){
			$(".left-box").append($(this).removeClass('selected-item'));
			getSelected();
			page.leftAndRightBind();
		});
	},
	removeBtnBind:function(){
		$(".add-one").bind("click",function(){
			$(".right-box").append($(".left-box").find('.selected-item').removeClass('selected-item'));
			getSelected();
		});
		$(".add-all").bind("click",function(){
			$(".right-box").append($(".left-box").find('.item').removeClass('selected-item'));
			getSelected();
		});
		$(".remove-one").bind("click",function(){
			$(".left-box").append($(".right-box").find('.selected-item').removeClass('selected-item'));
			getSelected();
		});
		$(".remove-all").bind("click",function(){
			$(".left-box").append($(".right-box").find('.item').removeClass('selected-item'));
			getSelected();
		});
	}
}
function getSelected(){
	approver_id_arr_old = [];
	$(".right-box li").each(function(){
		approver_id_arr_old.push($(this).attr("value"));
	});
}
function queryApprovalProcessSet(){
	var my_type;
	if(window.location.href.indexOf("attendApprovalProcess")>-1){
		my_type="1,2,3";//考勤
	}
	if(window.location.href.indexOf("attendApprovalProcessAccount")>-1){
		my_type="4,5,6";//财务
	}
	table=ajaxUtil.dataTablesAjax("queryTable",10,"dingTalk/queryApprovalProcessSet",
			{"type":my_type},[
			{"data":"type",render: function (data) {
				switch(data){
				case 1:return "请假审批";break;
				case 2:return "补卡审批";break;
				case 3:return "加班审批";break;
				case 4:return "报销审批";break;
				case 5:return "行政处罚审批";break;
				case 6:return "行政奖励审批";break;
				}
			}},
			{"data":"approvalName"},
			{
				"data":"approverIds",render:function(data){
					return "<button value='"+data+"' class='update btn btn-secondary radius'>修改</button> &nbsp;&nbsp; <button class='delete btn btn-danger radius'>删除</button>";
				}
			}
	]);
}

